package com.solvePlat.controller;

import com.solvePlat.entity.Document;
import com.solvePlat.entity.base.BaseResponse;
import com.solvePlat.service.Impl.UploadFileDealWithServiceImpl;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@RestController
@RequestMapping("/uploadFile")
public class UploadFileController {

    @Resource
    private UploadFileDealWithServiceImpl uploadFileDealWithService;

    @RequestMapping("/upload")
    public BaseResponse UploadImage(HttpServletRequest req,MultipartFile file) throws Exception{

        BaseResponse<Document> baseResponse = new BaseResponse<Document>();
        Document document = uploadFileDealWithService.uploadFileToServer(file,req);
        baseResponse.setData(document);
        baseResponse.setSuccess(true);
        baseResponse.setMsg("上传成功!");
        return baseResponse;
    }


    @GetMapping(value = "/showImage",produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public BufferedImage showImage(HttpServletRequest req,Document document) throws Exception {
        return uploadFileDealWithService.getImage(document,req);
    }

}
